package user

/* 
 * 用户管理
 * author cameron
 */

import (
	_ "fmt"
	"strconv"
	"gobackstage/code/controllers"
	."gobackstage/code/models/user"
	_ "strings"
)

type UserController struct{
	controllers.BaseController	
}

func (this *UserController) Get()  {

	_,_,userlist := ListAll()

	this.Data["userlist"] = userlist

	this.TplName = "admin/user/listall.html"
}

func (this *UserController) ApiListAll()  {

	//取要取得的数据表字典
	maps := make(map[string]string)
	maps["1"] = "id"
	maps["2"] = "username"
	maps["3"] = "password"
	maps["4"] = "status"

	//分页
	start := this.GetString("start")
	length := this.GetString("length");

	//排序
	sortFieldIndex := this.GetString("order.0.column");
	sortRule := this.GetString("order.0.dir");
	sortFieldName := maps[sortFieldIndex];

	//取得过虑条件
	username := this.GetString("username")

	//总条数
	count,_ := ListCount()

	if username == "" {
		//取得表数据
		_,_,userlist := ListAll()

		this.Data["json"] = map[string]interface{}{"draw": 0, "recordsTotal": count, "recordsFiltered": count, "data": userlist}
	
	}else{
		//取得表数据
		_,_,userlist := ListAllConn(username)
		countconn,_ := ListCountConn(username)
		this.Data["json"] = map[string]interface{}{"draw": 0, "recordsTotal": count, "recordsFiltered": countconn, "data": userlist}
	
	}
	
    this.ServeJSON()

	this.Ctx.WriteString(start+length+sortFieldName+sortRule) //@gewen?后期需要改变

}

func (this *UserController) AddPage() {

	this.TplName = "admin/user/add.html"

}

func (this *UserController) AddFunc() {

	username := this.GetString("username")
	password := this.GetString("password")

	var user User
	user.Username = username
	user.Password = password
	user.Status = 1
	_,err := AddOne(user)

	if err!=nil{
		this.Data["json"] = map[string]interface{}{"type": "error", "content":"添加失败"}
	}

	this.Data["json"] = map[string]interface{}{"type": "success", "content":"添加成功"}

	this.ServeJSON()

}

func (this *UserController) EditPage() {
	
	ids := this.GetString("id")
	id,_ := strconv.Atoi(ids)
	
	user,_ := ListOne(id)	
	this.Data["username"] = user.Username
	this.Data["id"] = user.Id
	this.Data["password"] = user.Password
	this.Data["status"] = user.Status

	this.TplName = "admin/user/edit.html"

}

func (this *UserController) EditFunc()  {

	id := this.GetString("id")
	username := this.GetString("username")
	password := this.GetString("password")
	status := this.GetString("status")

	ids,_ := strconv.Atoi(id)
	statuss,_ := strconv.Atoi(status)

	var user User
	user.Id = ids
	user.Username = username
	user.Status = statuss
	user.Password = password

	_,err := UpdateOne(user,)

	if err!=nil{
		this.Data["json"] = map[string]interface{}{"type": "error", "content":"修改失败"}
	}

	this.Data["json"] = map[string]interface{}{"type": "success", "content":"修改成功"}

	this.ServeJSON()
	
}


func (this *UserController) DelFunc() {

	id := this.Ctx.Input.Param(":id")
	b,err := strconv.Atoi(id)
	if err != nil{
		panic("删除失败")
	}
	_,err = DelOne(b)

	if err != nil {
		panic("删除失败")
	}

	this.Redirect("/admin/user",302)

}

func (this *UserController) ApiEditStatus()  {
	
	id := this.GetString("id")
	status := this.GetString("status")

	ids,_ := strconv.Atoi(id)
	statuss,_ := strconv.Atoi(status)

	var user User
	user.Id = ids
	user.Status = statuss

	_,err := UpdateStatus(user)

	if err!=nil{
		this.Data["json"] = map[string]interface{}{"type": "error", "content":"修改状态失败"}
	}

	this.Data["json"] = map[string]interface{}{"type": "success", "content":"修改状态成功"}

    this.ServeJSON()

}

func (this *UserController) ApiDel()  {
	
	id := this.GetString("id")

	ids,_ := strconv.Atoi(id)

	_,err := DelOne(ids)

	if err!=nil{
		this.Data["json"] = map[string]interface{}{"type": "error", "content":"删除失败"}
	}

	this.Data["json"] = map[string]interface{}{"type": "success", "content":"删除成功"}

	this.ServeJSON()
	
}

func (this *UserController) ApiDelAll()  {
	
	id := this.GetString("id")
	_,err := DelAll(id)

	if err!=nil{
		this.Data["json"] = map[string]interface{}{"type": "error", "content":"删除失败"}
	}

	this.Data["json"] = map[string]interface{}{"type": "success", "content":"删除成功"}

	this.ServeJSON()
	
}
